package com.itfreer.form.service.metadata;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.itfreer.form.api.BaseDao;
import com.itfreer.form.api.imp.BaseServiceImp;
import com.itfreer.form.dao.metadata.DictionaryDao;
import com.itfreer.form.dictionary.base.DictionaryItem;
import com.itfreer.form.dictionary.cache.DictionaryCache;
import com.itfreer.form.entity.metadata.DictionaryEntity;
import com.itfreer.form.service.metadata.DictionaryService;

/**
 * 定义字典服务
 */
@Component("dictionaryServiceImp")
public class DictionaryServiceImp extends BaseServiceImp<DictionaryEntity> implements DictionaryService {

	@Autowired
	private DictionaryDao dictionaryDao;

	@Override
	protected BaseDao<DictionaryEntity> getDao() {
		return dictionaryDao;
	}

	/**
	 * 字典缓存服务
	 */
	@Autowired(required = false)
	private DictionaryCache dictionaryCache;

	@PostConstruct
	public void initDictionary() {
		if (dictionaryCache == null) {
			return;
		}

		List<DictionaryEntity> list = getDao().getEntitys(null, null, null, 500000, 1);
		if (list != null && list.size() > 0) {
			HashMap<String, List<DictionaryItem>> cacheList = new HashMap<String, List<DictionaryItem>>();

			for (DictionaryEntity entity : list) {
				String dicName = entity.getDicName().toLowerCase();
				DictionaryItem item = convert(entity);

				if (cacheList.containsKey(dicName)) {
					cacheList.get(dicName).add(item);
				} else {
					List<DictionaryItem> clist = new ArrayList<DictionaryItem>();
					clist.add(item);
					cacheList.put(dicName, clist);
				}
			}

			for (String dic : cacheList.keySet()) {
				dictionaryCache.init(dic, cacheList.get(dic));
			}
		}
	}

	public void addDictionary(String dicName, DictionaryItem dictionary) {
		if (dictionaryCache == null) {
			return;
		}

		dictionaryCache.add(dicName.toLowerCase(), dictionary);
	}

	public void updateDictionary(String dicName, DictionaryItem dictionary) {
		if (dictionaryCache == null) {
			return;
		}

		dictionaryCache.update(dicName.toLowerCase(), dictionary);
	}

	public void deleteDictionary(String dicName, String guid) {
		if (dictionaryCache == null) {
			return;
		}

		dictionaryCache.delete(dicName.toLowerCase(), guid);
	}

	private DictionaryItem convert(DictionaryEntity entity) {
		DictionaryItem toEntity = new DictionaryItem();
		BeanUtils.copyProperties(entity, toEntity);
		return toEntity;
	}

	/**
	 * 添加实体
	 * 
	 * @param entity
	 *            添加实体
	 * @return
	 */
	@Override
	public DictionaryEntity add(DictionaryEntity entity) {
		entity = super.add(entity);
		addDictionary(entity.getDicName(), convert(entity));
		return entity;
	}

	/**
	 * 更新实体
	 * 
	 * @param guid
	 *            唯一值
	 * @param entity
	 *            实体
	 * @return
	 */
	@Override
	public DictionaryEntity update(DictionaryEntity entity) {
		entity = super.update(entity);
		updateDictionary(entity.getDicName(), convert(entity));
		return entity;
	}

	/**
	 * 删除实体
	 * 
	 * @param guid
	 *            唯一值
	 */
	@Override
	public void delete(String guid) {
		DictionaryEntity entity = super.getEntity(guid);
		super.delete(guid);
		deleteDictionary(entity.getDicName(), guid);
	}

	/**
	 * 指量删除实体
	 * 
	 * @param guid
	 *            唯一值集
	 */
	@Override
	public void deletes(String guids) {
		String[] ids = guids.split(",");
		for (String id : ids) {
			this.delete(id);
		}
	}
	
	/**
	 * 删除实体
	 * 
	 * @param guid
	 *            唯一值
	 */
	@Override
	public void delete(String dn, String guid) {
		DictionaryEntity entity = super.getEntity(guid);
		dictionaryDao.deleteEx(dn, guid);
		deleteDictionary(entity.getDicName(), guid);
	}
	
	/**
	 * 指量删除实体
	 * 
	 * @param guid
	 *            唯一值集
	 */
	@Override
	public void deletes(String dn, String guids) {
		String[] ids = guids.split(",");
		for (String id : ids) {
			this.delete(dn, id);
		}
	}
	
	/**
	 * 根据字典名称，获取所有字典数据
	 * @param dicName
	 * @return
	 */
	@Override
	public List<DictionaryEntity> getEntitys(String dicName) {
		Map<String, Object> where = new HashMap<String, Object>();
		where.put("dicName", dicName);
		Map<String, Integer> order = new HashMap<String, Integer>();
		order.put("code", 0);
		return super.getEntitys(null, where, order, 100000, 1);
	}
}